package com.wesley.growth.itext7;

import com.itextpdf.kernel.pdf.*;

/**
 * Itext7Util
 *
 * @author WangPanYong
 * @since 2024/03/22
 */
public final class Itext7Util {

    public static void encryptPdf(String src, String dest, String password)  {
        try {
            PdfDocument pdfDoc = new PdfDocument(
                    new PdfReader(src),
                    new PdfWriter(dest, new WriterProperties().setStandardEncryption(
                            password.getBytes(),
                            password.getBytes(),
                            EncryptionConstants.ALLOW_PRINTING,
                            EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA))
            );
            pdfDoc.close();
        } catch (Exception e) {
            String msg = "Pdf加密失败! 错误信息: " + e.getMessage();
            System.out.println(msg);
            e.printStackTrace();
        }
    }
}
